package ;
import content.ContentManager;
import easelhx.display.Shape;
import easelhx.display.Stage;
import haxe.Timer;
import js.DomCanvas;
import js.Lib;
import js.Dom.HtmlDom;
import params.GlobalParams;
import tools.ASprite;
import tools.batcher.Batcher;
import tools.BoundProvider;
import ui.Background;
import ui.Footer;
import ui.Header;
import ui.nav.Nav;

/**
 * v8 / 2012
 * @author PRASS
 */
 
class Main
{
	private var bg 		: Background;
	private var header	: Header;
	private var content	: ContentManager;
	private var footer	: Footer;
	private var batcher	: Batcher;
	private var safety	: Timer;
	
	static private var loader : JQuery;
	
	static function main() 
	{
		loader = new JQuery("#mainLoader");
	}
	
	static function start()
	{		
		var browserTest : String = UserAgent.navigator.appVersion;
				
		if (browserTest.charAt(0) != "5" && !Lib.isOpera )
		{
			loader.css("width", "500px");
			loader.css("background-image", "none");
			loader.append("I'm sorry but HTML5 is not supported by your browser.<br/>You should upgrade.");
			return;
		}
		
		new Main();
	}
	
	public function new ()
	{	
		batcher = new Batcher( this, build );
		batcher.addItem(GlobalParams.BG_MAIN);
		batcher.addItem(GlobalParams.BG_WORKS);
		batcher.addItem(GlobalParams.BG_ABOUT);
		batcher.addItem(GlobalParams.BG_CONTACT);
		batcher.addItem(GlobalParams.BG_LINKS);
		batcher.start();
		
		safety = new Timer(25000);
		safety.run = build;
	}
	
	private function build() : Void
	{
		safety.stop();
		safety = null;
		
		batcher.killed = true;
		
		header 	= new Header();
		content = new ContentManager();
		footer 	= new Footer();
		bg		= new Background();
		
		Lib.window.onresize = function(e) { onResize(); } ;
		onResize();
			
		bg.display();
		header.display();
		footer.display();
		
		loader.css("display", "none");
		loader = null;
		
	}

	private function onResize() : Void
	{
		BoundProvider.getBounds();
		var w : Int = BoundProvider.winW;
		var h : Int = BoundProvider.winH;
		
		bg.resize(w, h, BoundProvider.bgW, BoundProvider.bgH, BoundProvider.bgX, BoundProvider.bgY);
		header.resize(h);
		footer.resize(w, h);
		content.resize(w, h);
	}
	
}